<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {_formlist} function plugin
 *
 * Type:     function<br>
 * Name:     _formlits<br>
 * Date:     Oct 9, 2008<br>
 * Purpose:  make an list html table from an array of data<br>
 * Input:<br>
 *         - from = array to loop through
 *         - cols = number of columns, comma separated list of column names
 *                  or array of column names
 *         - inner = inner loop (default "cols": print $loop line by line,
 *                   $loop will be printed column by column otherwise)
 *
 *
 * Examples:
 * <pre>
 * {table from=$data}
 * {table from=$data cols=4}
 * {table from=$data cols="first,second,third"}
 * </pre>
 * @author Alex Yaroshevich <qfox@ya.ru>
 * @version 0.1a
 * @link http://docs.commercial.su/view:smarty {_form}
 * @param array
 * @param Smarty
 * @return string
 */
function smarty_function__form($params, &$smarty)
{
	if( !is_array( $params ) || empty( $params ) ) return;
	
	$options = isset( $params['options'] ) ? json::decode( $params['options'] ) : array( );
	unset( $params['options'] );
	
	$fields = array( );
	$rawFields = isset( $params['fields'] ) ? (is_string( $params['fields'] ) ? json::decode( $params['fields'] ) : $params['fields'] ) : $params;
	
	foreach( $rawFields as $k => $v )
	{
		$field = is_string( $v ) ? json::decode( $v ) : ( is_array( $v ) ? $v : null );
		//var_dump( array( $k => $v, 'field' => $field ) );
		if( is_null($field) || !is_array($field) ) parse_str( $v, $field );
		$fields[ $k ] = $field;
	}
	
	$_r = view::instance( ) ->display( "bits/form.sma", array( 'fields' => $fields, 'options' => $options ) );
	
	return $_r;
}

?>